package com.homedo.odin.solution.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.service.BaseService;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.core.common.util.PinyinUtil;
import com.homedo.odin.solution.dao.BrandOwnerMapper;
import com.homedo.odin.solution.enumcode.brand.BrandSortEnum;
import com.homedo.odin.solution.model.dto.BrandEntranceBean;
import com.homedo.odin.solution.model.dto.BrandOwnerBean;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.entity.BrandOwner;
import com.homedo.odin.solution.model.request.pc.brand.BrandListRequestBean;
import com.homedo.odin.solution.model.response.pc.brand.BrandOwnerDetailResponse;
import com.homedo.odin.solution.model.response.pc.brand.BrandOwnerPageResponseBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p> 品牌 服务实现类 </p>
 *
 * @author HarveyDing
 * @since 2018-03-08
 */
@Service
public class BrandOwnerServiceImpl extends BaseService<BrandOwnerMapper, BrandOwner> {

    public BrandOwner findById(Integer brandId) {
        BrandOwner brandOwner = new BrandOwner();
        brandOwner.setId(brandId);
        brandOwner.setIsDel(0);
        return baseMapper.selectOne(brandOwner);
    }

    public BrandOwner getBrandOwnerById(Integer id) {
        return baseMapper.getBrandOwnerById(id);
    }

    public List<BrandOwner> getBrandCal(Integer number) {
        return Optional.ofNullable(baseMapper.getBrandCal(number)).orElse(new ArrayList<>());
    }

    public List<BrandOwnerBean> queryBrandFirmsEntrance() {
        return baseMapper.queryBrandFirmsEntrance();
    }

    public PageInfo<BrandEntranceBean> queryBrandEntranceList(String sortName, String sort, PageBean pageBean) {
        if (StringUtils.isNotEmpty(sortName) && StringUtils.isNotEmpty(sort)) {
            if (BrandSortEnum.SOLUTION_COUNT.getDisplayValue().equals(sortName) || BrandSortEnum.SORT.getDisplayValue()
                    .equals(sortName)) {
                PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize(), sortName + " " + sort);
            }
        } else {
            PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        }
        return new PageInfo<>(baseMapper.queryBrandEntranceList());
    }

    public BrandOwner queryLateBrand() {
        return baseMapper.queryLateBrand();
    }

    /**
     * 根据品牌分类获取品牌的数据
     */
    public List<BrandOwnerDetailResponse> getBrandsByType(Integer typeId) {
        return convert2Response(baseMapper.getBrandsByType(typeId));
    }

    private List<BrandOwnerDetailResponse> convert2Response(List<BrandOwner> queryResult) {
        List<BrandOwnerDetailResponse> responseList = new ArrayList<>();
        for (BrandOwner brandOwner : queryResult) {
            BrandOwnerDetailResponse brandOwnerDetail = new BrandOwnerDetailResponse();
            brandOwnerDetail.setBrandId(brandOwner.getId());
            brandOwnerDetail.setBrandName(brandOwner.getBrandName());
            brandOwnerDetail.setBrandIcon(brandOwner.getBrandIcon());
            if (StringUtils.isNotEmpty(brandOwner.getBrandName())) {
                brandOwnerDetail.setFirstLetter(
                        PinyinUtil.getPinYinHeadChar(brandOwner.getBrandName()).substring(0, 1).toUpperCase());
            }
            responseList.add(brandOwnerDetail);
        }
        return responseList;
    }

    public BrandOwnerPageResponseBean getBranOwners(BrandListRequestBean requestBean) {
        PageHelper.startPage(requestBean.getPageNum(), requestBean.getPageSize());
        PageInfo<BrandOwnerDetailResponse> pageInfo = new PageInfo<>(
                baseMapper.getBrandOwners(requestBean.getBrandName(), requestBean.getIsMap()));
        if (requestBean.isShowFirstLetter()) {
            List<BrandOwnerDetailResponse> list = pageInfo.getList();
            list.forEach(brandOwner -> {
                if (StringUtils.isNotEmpty(brandOwner.getBrandName())) {
                    brandOwner.setFirstLetter(
                            PinyinUtil.getPinYinHeadChar(brandOwner.getBrandName()).substring(0, 1).toUpperCase());
                }
            });
        }
        return BeanUtils.transfrom(BrandOwnerPageResponseBean.class, pageInfo);
    }
}
